package com.xian.jdbc;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

//获取mysql数据结构

/**
 * java -cp *.jar MainClass  方式最好
 * 或者
 * java -jar Hive-2-3-V1.0-jar-with-dependencies.jar  jdbc:mysql://172.18.153.10:3306/carorder?user=pubuser&password=Z883eD21eo21q0E7HAkftKXOZ
 */
public class GetMysql {
    public static void main(String[] args) throws Exception {
        GetMysql instance = new GetMysql();
        Class.forName("com.mysql.jdbc.Driver");
        String url = args[0];
        //String url = "jdbc:hive2://172.18.224.152:10000";
        System.out.println("传入url="+url);
        String dbName = args[1];
        System.out.println("传入dbName="+dbName);
        Connection con = DriverManager.getConnection(url);//密码为空
        Statement sta = con.createStatement();


        //获取所有数据库
        List<String> dbNames = instance.getDBNames(sta);
        System.out.println("有权限访问的库:"+dbNames);

        //获取某个数据库所有表 (获取所有库的所有表)
        List<String> tableNames = instance.getTableNames(sta, dbName);



        //获取某个表建表语句
        System.out.println("==========所有表建表语句==========");
        for (String tableName : tableNames) {
            String createTableSQL = instance.getCreateTableSQL(sta, tableName);
            System.out.println(createTableSQL);
        }

        System.out.println("！！！运行完毕！！！");




        //关闭连接
        sta.close();
        con.close();



    }

    //获取所有数据库
    public List<String> getDBNames(Statement statement) throws SQLException, ClassNotFoundException {
        List<String> dbNames = new ArrayList<String>(); //存放所有数据库名称
        String sql = "show databases";
        ResultSet resultSet = statement.executeQuery(sql);
        while (resultSet.next()) {
            String dbname = resultSet.getString(1); //用索引获取值
            //String dbname = resultSet.getString("database_name"); //或者用字段获取值
            //System.out.println(dbname);
            dbNames.add(dbname);
        }
        resultSet.close();
        return dbNames;
    }

    //获取某个库所有表名
    public List<String> getTableNames(Statement statement, String dbName) throws ClassNotFoundException, SQLException {
        List<String> tbleNames = new ArrayList<String>(); //存放数据库所有表名

        statement.execute("use "+ dbName);

        String sql = "show tables";
        ResultSet resultSet = statement.executeQuery(sql);
        while (resultSet.next()) {
            String tableName = resultSet.getString(1); //用索引获取值
            //System.out.println(dbName+"."+tableName);
            tbleNames.add(dbName+"."+tableName);
        }
        resultSet.close();
        return tbleNames;
    }


    //获取某个表建表语句
    public String getCreateTableSQL(Statement statement, String tableName) throws ClassNotFoundException, SQLException {
        StringBuilder sb = new StringBuilder(); //存放建表语句

        String sql = "show create table " + tableName;
        ResultSet resultSet = statement.executeQuery(sql);

        while (resultSet.next()) { //主要获取到两个字段  "Table"  "Create Table"
            String t = resultSet.getString(1); //用索引获取值
            String createTable = resultSet.getString(2); //用索引获取值
            //System.out.println(s);
            sb.append(createTable);
        }


        resultSet.close();

        //替换表名为带库名即自己传入的   如： CREATE TABLE `passengerfreepaytask` (  替换为 CREATE TABLE `carorder.passengerfreepaytask`
        //TMD 创建表时mysql不能再额外带库名 不然当整体表名
        //String createSql = sb.toString().replace(t, tableName)+";";

        return sb.append(";").toString();
    }





}
